package org.crmsystem.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.dao.PermissionMapper;
import org.crmsystem.model.Permission;
import org.crmsystem.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 权限服务层实现
 * @author 孙艳平
 *
 */
@Service
public class PermissionServiceImpl implements IPermissionService {
	@Autowired
	private PermissionMapper permissionMapper;
	private Logger logger=LogManager.getLogger(PermissionServiceImpl.class);
	/**
	 * 根据id获取权限详细信息
	 */
	public Permission getPermissionById(int id) {
		return permissionMapper.getPermissionById(id);
	}

	/**
	 * 多条件查询获取权限列表
	 */
	public List<Permission> getPermission(Permission permission) {
		return permissionMapper.getPermission(permission);
	}

	/**
	 * 添加权限
	 */
	public int addPermission(Permission permission) {
		return this.permissionMapper.addPermission(permission);
	}

	/**
	 * 删除权限
	 */
	@Transactional
	public int delPermission(int id) {
		int flag=this.permissionMapper.updateAllRolePermission(id);
		if(flag>0){
			return this.permissionMapper.delPermission(id);
		}else{
			return -1;
		}
	}

	/**
	 * 更新权限
	 */
	public int updatePermission(Permission permission) {
		return this.permissionMapper.updatePermission(permission);
	}

	/**
	 * 根据角色Id,获取角色已获得的权限
	 */
	public List<Permission> getPermissionByRoleId(int roleId) {
		return this.permissionMapper.getPermissionByRoleId(roleId);
	}

	/**
	 * 根据角色Id,获取角色未获取的权限
	 */
	public List<Permission> getPermissionByRoleIdNo(int roleId) {
		return this.permissionMapper.getPermissionByRoleIdNo(roleId);
	}

	/**
	 * 查询指定角色是否获得指定权限
	 */
	public int getPermissionHave(Map map) {
		return this.permissionMapper.getPermissionHave(map);
	}

	/**
	 * 给指定角色授予权限
	 */
	public int addRolePermission(Map map) {
		return this.permissionMapper.addRolePermission(map);
	}

	/**
	 * 恢复指定角色的指定权限
	 */
	public int updateRolePermission(Map map) {
		return this.permissionMapper.updateRolePermission(map);
	}

	/**
	 * 删除指定角色的指定权限
	 */
	public int deleteRolePermission(Map map) {
		return this.permissionMapper.deleteRolePermission(map);
	}
}
